package com.yanqu.road.server.manger;

import com.yanqu.road.entity.servercenter.ChannelInfo;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.utils.ConfigHelper;
import com.yanqu.road.utils.kafka.KafKaHelper;
import com.yanqu.road.utils.string.StringUtils;

public class KafKaHelperMgr extends TempMgr {

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        String cid = ConfigHelper.getPath("channelId");
        int channelId;
        if(StringUtils.isNullOrEmpty(cid)){
            long serverId = ConfigHelper.getLong("serverId");
            ServerInfo serverInfo = ServerListMgr.getServerInfo(serverId);
            if(serverInfo == null){
                return true;
            }
            channelId = serverInfo.getChannelId();
        }else {
            channelId = Integer.parseInt(cid);
        }
        ChannelInfo channelInfo = ServerListMgr.getChannelInfo(channelId);
        if(channelInfo == null){
            return true;
        }
        KafKaHelper.initProducer(channelInfo.getKafkaServers());
        getLogger().info("kafka 初始化地址：{}", channelInfo.getKafkaServers());
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        KafKaHelper.getProducer().flush();
        KafKaHelper.getProducer().close();
        return true;
    }
}
